package org.darkforest.merchant.controller;


import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.CartService;
import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.common.services.GoodsClassService;
import org.darkforest.common.services.SettingService;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.*;
import org.darkforest.enums.AjaxMsgTypeEnum;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.merchant.constants.MerchantConstant;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.*;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "goods")
public class GoodsController extends WebController<GoodsController> {

    Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private GoodsClassStapleMapper goodsClassStapleMapper;

    @Autowired
    private GoodsClassRedisService goodsClassRedisService;

    @Autowired
    private SpecMapper specMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private SpecValueMapper specValueMapper;

    @Autowired
    private SettingService settingService;
    @Autowired
    private GoodsClassService goodsClassService;

    @Autowired
    private AttributeMapper attributeMapper;

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private GoodsTagMapper goodsTagMapper;

    @Autowired
    private ActiveAreaGoodsMapper activeAreaGoodsMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private StoreMapper storeMapper;
    /**
     * 发布商品第一步，进入选择商品分类页面
     * @return
     */
    @AuthPassport
    @RequestMapping(value="goodsAdd")
    public ModelAndView publishGoods(HttpServletRequest request) throws Exception {
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

//        List<Goss> goodsClassList =  goodsService.publishGoods();
        //改为从redis中获取数据
//        List<GoodsClass> goodsClassList = goodsClassRedisService.getGoodsClassList(null);
//        if(store.getBindAllGc()==1){ //判断店铺是否绑定全部分类
//            goodsClassList  = goodsClassRedisService.getGoodsClassList(null);
//        }else{
        List<GoodsClass> goodsClassList =goodsClassService.findStoreBindClass(store.getStoreId(), 1,null);

//        }
        //获取商户常用分类
        GoodsClassStaple goodsClassStaple = new GoodsClassStaple();
        goodsClassStaple.setMemberId(store.getMemberId());
        List<GoodsClassStaple> goodsClassStapleList =goodsClassStapleMapper.findClassStapleByCondition(goodsClassStaple);
        ModelAndView modelAndView =  new ModelAndView("goods/add_step1");
        modelAndView.addObject("goodsClassList",goodsClassList);
        modelAndView.addObject("goodsClassStapleList",goodsClassStapleList);
        return modelAndView;
    }

    //常用分类选择
    @AuthPassport
    @RequestMapping("inCommonUseClass")
    public @ResponseBody GoodsInCommonClassVO  inCommonUseClass(HttpServletRequest request){
        Integer stapleid = Integer.parseInt(request.getParameter("stapleid"));
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        GoodsClassStaple goodsClassStaple =goodsClassStapleMapper.selectByPrimaryKey(stapleid);
        List<GoodsClass> goodsClassListL1 =goodsClassService.findStoreBindClass(store.getStoreId(), 1, null);

//        List<GoodsClass> goodsClassListL1 = goodsClassRedisService.getGoodsClassList(null);
        List<GoodsClass> goodsClassListL2 = goodsClassRedisService.getGoodsClassList(goodsClassStaple.getGcId1());
        List<GoodsClass> goodsClassListL3 = goodsClassRedisService.getGoodsClassList(goodsClassStaple.getGcId2());




        GoodsInCommonClassVO goodsInCommonClassVO = new GoodsInCommonClassVO();

        StringBuilder sbd1 = new StringBuilder();
        StringBuilder sbd2 = new StringBuilder();
        StringBuilder sbd3 = new StringBuilder();

        for(GoodsClass goodsClass:goodsClassListL1){
            if(!goodsClass.getGcId().equals(goodsClassStaple.getGcId1())){
                sbd1.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:1, tid:0}\" nctype=\"selClass\"> <a class=\"\" href=\"javascript:void(0)\"><span class=\"has_leaf\"><i class=\"icon-angle-right\"></i>"+goodsClass.getGcName()+"</span></a> </li>");
            }else{
                sbd1.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:1, tid:0}\" nctype=\"selClass\"> <a class=\"classDivClick\" href=\"javascript:void(0)\"><span class=\"has_leaf\"><i class=\"icon-angle-right\"></i>"+goodsClass.getGcName()+"</span></a> </li>");
            }
        }
        for(GoodsClass goodsClass:goodsClassListL2){
            if(!goodsClass.getGcId().equals(goodsClassStaple.getGcId2())){
                sbd2.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:2, tid:0}\" nctype=\"selClass\"> <a class=\"\" href=\"javascript:void(0)\"><span class=\"has_leaf\"><i class=\"icon-angle-right\"></i>"+goodsClass.getGcName()+"</span></a> </li>");
            }else{
                sbd2.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:2, tid:0}\" nctype=\"selClass\"> <a class=\"classDivClick\" href=\"javascript:void(0)\"><span class=\"has_leaf\"><i class=\"icon-angle-right\"></i>"+goodsClass.getGcName()+"</span></a> </li>");
            }
        }
        for(GoodsClass goodsClass:goodsClassListL3){
            if(!goodsClass.getGcId().equals(goodsClassStaple.getGcId3())){
                sbd3.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:3, tid:0}\" nctype=\"selClass\"> <a class=\"\" href=\"javascript:void(0)\"><span class=\"has_leaf\">"+goodsClass.getGcName()+"</span></a> </li>");
            }else{
                sbd3.append("<li class=\"\" onclick=\"selClass($(this));\" data-param=\"{gcid:"+goodsClass.getGcId()+", deep:3, tid:0}\" nctype=\"selClass\"> <a class=\"classDivClick\" href=\"javascript:void(0)\"><span class=\"has_leaf\">"+goodsClass.getGcName()+"</span></a> </li>");
            }
        }


        goodsInCommonClassVO.setType_id(goodsClassStaple.getTypeId()+"");

        goodsInCommonClassVO.setDone("done");
        goodsInCommonClassVO.setOne(sbd1.toString());
        goodsInCommonClassVO.setTwo(sbd2.toString());
        goodsInCommonClassVO.setThree(sbd3.toString());
        return goodsInCommonClassVO;
    }

    @AuthPassport
    @RequestMapping("delInCommonUseClass")
    public @ResponseBody GoodsInCommonClassVO delInCommonUseClass(HttpServletRequest request){
        Integer stapleid = Integer.parseInt(request.getParameter("stapleid"));
        goodsClassStapleMapper.deleteByPrimaryKey(stapleid);
        GoodsInCommonClassVO goodsInCommonClassVO = new GoodsInCommonClassVO();
        goodsInCommonClassVO.setDone("true");
        return goodsInCommonClassVO;
    }

    /**
     * 发布商品第二步，填写商品详情 并且将商品的分类带入
     * @return
     */
    @AuthPassport
    @RequestMapping(value="goodsAddStep1")
    public ModelAndView publishGoods_step1(String gc1,String gc2,String gc3,String category,String gcName,HttpServletRequest request) throws Exception {
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        List<Brand> brandList = this.brandMapper.findBrandByCondition(new Brand(Integer.parseInt(gc1)));


        GoodsTag goodsTag = new GoodsTag();
        goodsTag.setPageSwitch(false);
        goodsTag.setTagState(1);
        List<GoodsTag> goodsTagList = goodsTagMapper.selectListByCondition(goodsTag);


        GoodsClassStaple goodsClassStaple = new GoodsClassStaple();
        goodsClassStaple.setGcId1(Integer.parseInt(gc1==null ||"".equals(gc1)?"0":gc1));
        goodsClassStaple.setGcId2(Integer.parseInt(gc2 == null || "".equals(gc2) ? "0" : gc2));
        goodsClassStaple.setGcId3(Integer.parseInt(gc3 == null || "".equals(gc3)?"0":gc3));

        List<GoodsClassStaple> goodsClassStapleList= goodsClassStapleMapper.findClassStapleByCondition(goodsClassStaple);
        ModelAndView modelAndView = new ModelAndView("goods/add_step2");
        if(goodsClassStapleList.size()<1) {
            goodsClassStaple.setMemberId(store.getMemberId());
            goodsClassStaple.setCounter(0);
            goodsClassStaple.setStapleName(gcName ==null?"":gcName);
            goodsClassStaple.setTypeId(0);
            goodsClassStapleMapper.insert(goodsClassStaple);
        }
        //获取规格
        Spec spec = new Spec();
        int classId  = 0;
        if (gc1!=null){
            classId = Integer.valueOf(gc1);
        }
        if (gc2!=null){
            classId = Integer.valueOf(gc2);
        }
        if (gc3!=null){
            classId = Integer.valueOf(gc3);
        }
        spec.setClassId(classId);
        spec.setPageSwitch(false);
        List<Spec> specList = specMapper.findSpec(spec);
        SpecValue specValue = new SpecValue();
        specValue.setGcId(Integer.valueOf(gc3));
        specValue.setStoreId(store.getStoreId());
        specValue.setPageSwitch(false);
        List<SpecValue> specValueList = specValueMapper.findSpecValue(specValue);
        Store storeInfo = storeMapper.selectStoreByPrimaryKey(store.getStoreId());
        Map<String,String> map = new HashMap<String, String>();
        map.put("gc1", gc1);
        map.put("gc2", gc2);
        map.put("gc3", gc3);

        map.put("class_id", category);
        map.put("gcName", gcName);
        map.put("storeClassId", storeInfo.getScId().toString());
        modelAndView.addObject("specList", specList);
        modelAndView.addObject("specValueList", specValueList);
        modelAndView.addObject("brandList", brandList);
        modelAndView.addObject("category",map);
        modelAndView.addObject("goodsTagList",goodsTagList);
        //获取分类的属性
        Attribute attribute = new Attribute();
        attribute.setTypeId(Integer.valueOf(gc3));
        modelAndView.addObject("attribute",attributeMapper.selectByCondition(attribute));

        return modelAndView;
    }

    /**
     * 发布商品第二步，填写商品详情
     * @return
     */
    @RequestMapping(value="goodsAddStep2")
    public String publishGoods_step2(@ModelAttribute("goods") GoodsCommonWithBLOBs goodsCommonWithBLOBs,HttpServletRequest request,String activeAreaType) throws Exception {
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        Integer freight = Integer.parseInt(request.getParameter("freight"));
        if(freight==1){
            goodsCommonWithBLOBs.setGoodsFreight(new BigDecimal(0));
        }else{
            goodsCommonWithBLOBs.setTransportId(0);
            goodsCommonWithBLOBs.setTransportTitle("");
        }
        goodsCommonWithBLOBs.setStoreId(store.getStoreId());
        goodsCommonWithBLOBs.setStoreName(store.getStoreName());

        String tmpDir = request.getSession().getServletContext().getRealPath("resources") + "/";
        goodsService.publishGoods_step2(goodsCommonWithBLOBs,tmpDir,activeAreaType);
        return "redirect:goodsAddStep3?id="+goodsCommonWithBLOBs.getGoodsCommonid();
    }

    @RequestMapping(value="ajaxFileUpload")
    public  @ResponseBody String ajaxFileUpload(MultipartHttpServletRequest multipartHttpServletRequeste, HttpServletResponse response) throws IOException {
        MultipartFile BrandPicturempf = multipartHttpServletRequeste.getFile("picture");
        String articlePicture = super.uploadPicsWithoutCompressSize(BrandPicturempf, multipartHttpServletRequeste, null, null);
        //返回结果
        return articlePicture;
    }

    @RequestMapping(value = "goodsAddStep3",method = RequestMethod.GET)
    public ModelAndView to_step3_page(@RequestParam Integer id,HttpServletRequest request){
        //根据goodsCommonId查询获取商品主图，商品规格信息，避免直接服务器端跳转方式出现刷新仍然提交表单的情况
        StoreSellerVo sellerVo=(StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        ModelAndView modelAndView = new ModelAndView("goods/add_step3","imgVo",new GoodsImagesVo());
        GoodsCommon goodsCommon = goodsService.getGoodsCommonById(id);
        List<GoodsImages> imagesList = goodsService.getGoodsImageByCommonId(goodsCommon.getGoodsCommonid());
        if("N;".equals(goodsCommon.getSpecValue())){
            logger.debug("goods_"+id+" has spec:"+1);
            modelAndView.addObject("specNum",0);
        }else{
            String[] specNames = goodsCommon.getSpecName().split(",");
            Map<String,Object> valueList = JSON.parseObject(goodsCommon.getSpecValue());
            Object array =  valueList.get(specNames[0]);
            modelAndView.addObject("specName", specNames[0]);
            modelAndView.addObject("specNum", array);
            //TODO 解析spec_value字段获取共有几个规格
        }
        modelAndView.addObject("storeId",sellerVo.getStoreId());
        modelAndView.addObject("goods",goodsCommon);
        modelAndView.addObject("imagesList",imagesList);
        return modelAndView;
    }

    /**
     * 发布商品第四步，发布成功后相关操作
     * @return
     */
    @RequestMapping(value="goodsAddStep4",method = RequestMethod.POST)
    public ModelAndView publishGoods_step4(@ModelAttribute("imgVo")GoodsImagesVo imgVo){
//        logger.debug("images:" + imgVo.getImages()[0][1].getGoodsImage());
        goodsService.addGoodsImagesList(imgVo);
        return new ModelAndView("goods/add_step4","id",imgVo.getCommonId());
    }



    /**
     * 商品搜索
     * @return
     */
    @RequestMapping(value="searchGoods")
    public ModelAndView searchGoods(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String reqPath = request.getParameter("reqPath");
        String keyword = request.getParameter("keyword");
        String searchType = request.getParameter("searchType");
        String type = request.getParameter("type");
        List<GoodsCommonWithBLOBs> goodsList = this.goodsService.searchGoods(encodeStr(keyword),searchType,type);
        ModelAndView modelAndView = new ModelAndView(reqPath);
        modelAndView.addObject("goodsList",goodsList);
        modelAndView.addObject("keyword",encodeStr(keyword));
        modelAndView.addObject("searchType", searchType);
        return modelAndView;
    }

    public static String encodeStr(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 在售商品列表
     * @return
     */
    @RequestMapping(value="goodsListOnline")
    public ModelAndView goodsListOnline(@RequestParam(required = false ,defaultValue = "") String searchKey,
                                        @RequestParam(required = false, defaultValue = "1") int p,
                                        @RequestParam(required = false ,defaultValue = "")  String searchColumn,
                                        HttpSession session) throws Exception {

        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();


        GoodsCommonSearchBean goodSearchBean = new GoodsCommonSearchBean();
        goodSearchBean.setInStatus("('1','2')");
        goodSearchBean.setGoodsVerify(1);
        goodSearchBean.setStart(p);
        goodSearchBean.setGoodsVerify(1);
        goodSearchBean.setGoodsType(0);

        goodSearchBean.setSearchColumn(searchColumn);
        goodSearchBean.setSearchKey(searchKey);
        goodSearchBean.setStoreId(storeId);
        goodSearchBean = this.goodsService.findGoodsInState(goodSearchBean);
        ModelAndView modelAndView = new ModelAndView("goods/goods_list");
        modelAndView.addObject("pcweb_site", settingService.getSettingInfo(SettingNameEnum.PCWEBSITE));
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }

    /**
     * 仓库商品列表
     * @return
     */
    @RequestMapping(value="goodsListOffline")
    public ModelAndView goodsListOffline(@RequestParam(required = false ,defaultValue = "") String searchKey,
                                         @RequestParam(required = false, defaultValue = "1") int p,
                                         @RequestParam(required = false ,defaultValue = "")  String searchColumn,
                                         HttpSession session) throws Exception {
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        GoodsCommonSearchBean goodSearchBean = new GoodsCommonSearchBean();
        goodSearchBean.setInStatus("('0','10')");
        goodSearchBean.setStart(p);
        goodSearchBean.setGoodsVerify(1);
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setSearchColumn(searchColumn);
        goodSearchBean.setSearchKey(searchKey);
        goodSearchBean.setStoreId(storeId);
        goodSearchBean = this.goodsService.findGoodsInState(goodSearchBean);
        ModelAndView modelAndView = new ModelAndView("goods/goods_list_offline");
        modelAndView.addObject("pcweb_site", settingService.getSettingInfo(SettingNameEnum.PCWEBSITE));
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }



    /**
     * 仓库商品列表
     * @return
     */
    @RequestMapping(value="goodsListVerifyRefuse")
    public ModelAndView goodsListVerifyRefuse(@RequestParam(required = false ,defaultValue = "") String searchKey,
                                              @RequestParam(required = false, defaultValue = "1") int p,
                                              @RequestParam(required = false ,defaultValue = "")  String searchColumn,
                                              HttpSession session) throws Exception {
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        GoodsCommonSearchBean goodSearchBean = new GoodsCommonSearchBean();
        goodSearchBean.setInStatus("('0','1','2')");
        goodSearchBean.setStart(p);
        goodSearchBean.setGoodsVerify(0);
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setSearchColumn(searchColumn);
        goodSearchBean.setSearchKey(searchKey);
        goodSearchBean.setStoreId(storeId);
        goodSearchBean = this.goodsService.findGoodsInState(goodSearchBean);
        ModelAndView modelAndView = new ModelAndView("goods/goods_list_verify_refuse");
        modelAndView.addObject("pcweb_site", settingService.getSettingInfo(SettingNameEnum.PCWEBSITE));
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }


    /**
     * 待审核商品列表
     * @return
     */
    @RequestMapping(value="goodsListVerify")
    public ModelAndView goodsListVerify(@RequestParam(required = false ,defaultValue = "") String searchKey,
                                        @RequestParam(required = false, defaultValue = "1") int p,
                                        @RequestParam(required = false ,defaultValue = "")  String searchColumn,
                                        HttpSession session) throws Exception {
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        GoodsCommonSearchBean goodSearchBean = new GoodsCommonSearchBean();
        goodSearchBean.setInStatus("('0','1','2')");
        goodSearchBean.setStart(p);
        goodSearchBean.setGoodsVerify(10);
        goodSearchBean.setGoodsType(0);

        goodSearchBean.setSearchColumn(searchColumn);
        goodSearchBean.setSearchKey(searchKey);
        goodSearchBean.setStoreId(storeId);
        goodSearchBean = this.goodsService.findGoodsInState(goodSearchBean);
        ModelAndView modelAndView = new ModelAndView("goods/goods_list_verify");
        modelAndView.addObject("pcweb_site", settingService.getSettingInfo(SettingNameEnum.PCWEBSITE));
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }

    /**
     * 修改商品信息
     * @return
     * @throws Exception
     */
    @RequestMapping(value="updateGoods")
    public String updateGoods(@ModelAttribute("goods") GoodsCommonWithBLOBs goodsCommonWithBLOBs,HttpServletRequest request, String activeAreaType)throws Exception{
        Integer freight = Integer.parseInt(request.getParameter("freight"));
        if(freight==1){
            goodsCommonWithBLOBs.setGoodsFreight(new BigDecimal(0));
        }else{
            goodsCommonWithBLOBs.setTransportId(0);
            goodsCommonWithBLOBs.setTransportTitle("");
        }

        goodsCommonWithBLOBs.setSpecName(goodsCommonWithBLOBs.getSpecName()==null?"N;":goodsCommonWithBLOBs.getSpecName());
        String specNames[]  = request.getParameterValues("specName");
        Map map = new HashMap();
        if(specNames!=null){
            for (int i=0;i<specNames.length;i++){
                map.put(specNames[i], request.getParameterValues("specValue" + i));
            }
            goodsCommonWithBLOBs.setSpecValue(JSON.toJSONString(map));
        }else {
            goodsCommonWithBLOBs.setSpecValue("N;");
        }
        this.goodsService.updateGoods(goodsCommonWithBLOBs, activeAreaType);
        return "success";
    }


    /**商品批量上架
     * @return
     * @throws Exception
     */
    @RequestMapping(value="uploadGoodsBatch")
    public @ResponseBody AjaxReturnData uploadGoodsBatch(HttpServletRequest request, String[] goodsCommonIds)throws Exception{
        AjaxReturnData data = new AjaxReturnData();
//        data.setUrl("goods/goodsListOffline");
        String goUri = request.getParameter("goUri");
        if (goUri == null || "".equals(goUri)) {
            goUri = "goodsListOffline";
        }
        data.setUrl("goods/" + goUri);
        data.setMessage("提交成功");
        data.setType(AjaxMsgTypeEnum.SUCC.getText());
        goodsService.modifyGoodsStatusByBatch(goodsCommonIds, MerchantConstant.GOODS_STUTS_ONLINE,"10");

//        String msg =super.getDialogXMLTemplete("商品上架成功", AjaxMsgTypeEnum.SUCC,"goods/uploadGoodsBatch",2);
//        return new XmlBody(msg);
        return data;
    }

    /**商品批量下架
     * @return
     * @throws Exception
     */
    @RequestMapping(value="dropGoodsBatch")
    public @ResponseBody AjaxReturnData dropGoodsBatch(String goodsCommonIds[])throws Exception{
        AjaxReturnData data = new AjaxReturnData();
        data.setUrl("goods/goodsListOnline");
        data.setMessage("商品下架成功");
//        AjaxMsgTypeEnum.SUCC.setText(data.getType());
        data.setType(AjaxMsgTypeEnum.SUCC.getText());
        goodsService.modifyGoodsStatusByBatch(goodsCommonIds, MerchantConstant.GOODS_STUTS_OFFLINE,"1");
        return data;
    }

    /**商品批量删除（修改状态）
     * @return
     * @throws Exception
     */
    @RequestMapping(value="deleteGoodsBatch")
    public @ResponseBody AjaxReturnData deleteGoodsBatch(HttpServletRequest request, String[] goodsCommonIds){
        AjaxReturnData data = new AjaxReturnData();
        //data.setUrl("goods/goodsListOnline");
        String goUri = request.getParameter("goUri");
        if (goUri == null || "".equals(goUri)) {
            goUri = "goodsListOnline";
        }
        data.setUrl("goods/" + goUri);
        data.setMessage("商品删除成功");
        data.setType(AjaxMsgTypeEnum.SUCC.getText());
        goodsService.modifyGoodsStatusByBatch(goodsCommonIds, MerchantConstant.GOODS_STUTS_DELETE,null);
        return data;
    }


    /**
     * 根据商品分类ID获取下级分类
     * @param gcId
     * @param deep
     * @return
     */

    @RequestMapping(value="getChildGoodsClass")
    public @ResponseBody List<GoodsClass> getChildGoodsClass(@RequestParam Integer gcId,Integer deep,HttpSession session){
//        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        StoreSellerVo store = (StoreSellerVo)session.getAttribute("sellerInfo");
        List<GoodsClass> goodsClassList = goodsClassRedisService.getGoodsClassList(gcId);
//        if(store.getBindAllGc()==1){
//            goodsClassList =  goodsClassRedisService.getGoodsClassList(gcId);
//        }else{
//             goodsClassList = goodsClassService.findStoreBindClass(store.getStoreId(),deep,gcId);
//
//        }
        return goodsClassList;
    }

    /**
     * 跳转到商品编辑页面
     * @return
     */
    @RequestMapping(value="toModifyGoods")
    public ModelAndView toModifyGoods(HttpServletRequest request, @RequestParam Integer id){
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        GoodsDetail goodsDetail = this.goodsService.getGoodsDetailById(id);
        List<Brand> brandList = this.brandMapper.findBrandByCondition(new Brand());
        Spec spec = new Spec();
        spec.setClassId(goodsDetail.getGcId3());
        spec.setPageSwitch(false);
        List<Spec> specList = specMapper.findSpec(spec);
        SpecValue specValue = new SpecValue();
        specValue.setGcId(Integer.valueOf(goodsDetail.getGcId3()));
        specValue.setStoreId(store.getStoreId());
        specValue.setPageSwitch(false);
        List<SpecValue> specValueList = specValueMapper.findSpecValue(specValue);
        Goods  goods = new Goods();
        goods.setGoodsCommonid(id);
        List<Goods> goodses = goodsMapper.findGoodsByCondition(goods);
        ModelAndView modelAndView =new ModelAndView("goods/modify_goods");
        GoodsTag goodsTag = new GoodsTag();
        goodsTag.setPageSwitch(false);
        goodsTag.setTagState(1);
        List<GoodsTag> goodsTagList = goodsTagMapper.selectListByCondition(goodsTag);
        ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
        activeAreaGoods.setGoodsCommonid(id);
        List<ActiveAreaGoods> activeAreaGoodsList = activeAreaGoodsMapper.selectByCondition(activeAreaGoods);
        String recommendType = "none";
        if (activeAreaGoodsList != null && activeAreaGoodsList.size() > 0) {
            recommendType = activeAreaGoodsList.get(0).getRecommendType();
        }
        modelAndView.addObject("goodsList",goodses);
        modelAndView.addObject("specList",specList);
        modelAndView.addObject("specValueList",specValueList);
        modelAndView.addObject("brandList", brandList);
        modelAndView.addObject("goodsInfo", goodsDetail);
        modelAndView.addObject("goodsTagList", goodsTagList);
        modelAndView.addObject("recommendType", recommendType);

        if(!goodsDetail.getSpecValue().equals("N;")){
            List<Object> valueList = new ArrayList<Object>();
            for (int i = 0; i < goodses.size(); i++) {
                valueList.add(goodses.get(i).getGoodsSpec());
            }
            modelAndView.addObject("valueList", valueList);
        }
        //获取分类的属性
//        if(goodsDetail.getGoodsAttr() !=null&&!goodsDetail.getGoodsAttr().equals("")){
//            modelAndView.addObject("attribute",JSON.parseArray(goodsDetail.getGoodsAttr(), Attribute.class));
//        }
        if(StringUtils.isNotEmpty(goodsDetail.getMobileBody())){
            JSONArray jsonArray = JSONArray.fromObject(goodsDetail.getMobileBody());
            List<MobileBody> mobileBodyList = (List<MobileBody>) JSONArray.toCollection(jsonArray, MobileBody.class);
            modelAndView.addObject("mobileBodyList", mobileBodyList);
//            String mbBody_str = "";
//            for (MobileBody mobile : mobileBodyList) {
//                if ("image".equals(mobile.getType())) {
//                    mbBody_str += "<img src='" + mobile.getValue() + "' width='100%'>";
//                }
//            }
//            goodsDetail.setMobileBody(mbBody_str);
        }

        Store storeInfo = storeMapper.selectStoreByPrimaryKey(goodsDetail.getStoreId());
        Map<String,String> map = new HashMap<String, String>();
        map.put("storeClassId", storeInfo.getScId().toString());
        modelAndView.addObject("category",map);
        return modelAndView;
    }


    /**
     * 编辑商品
     * @return
     */
    @RequestMapping(value="doModifyGoods")
    public String doModifyGoods(GoodsCommonWithBLOBs goodsCommonWithBLOBs, HttpServletRequest request, String activeAreaType){
        Integer freight = Integer.parseInt(request.getParameter("freight"));
        if(freight==1){
            goodsCommonWithBLOBs.setGoodsFreight(new BigDecimal(0));
        }else{
            goodsCommonWithBLOBs.setTransportId(0);
            goodsCommonWithBLOBs.setTransportTitle("");
        }
        goodsService.updateGoods(goodsCommonWithBLOBs, activeAreaType);
        return "redirect:toModifyGoodsPic?id="+goodsCommonWithBLOBs.getGoodsCommonid();
    }

    /**
     * 修改商铺图片
     * @param id
     * @return
     */
    @RequestMapping("toModifyGoodsPic")
    public ModelAndView toModifyGoodsPic(@RequestParam Integer id){
//根据goodsCommonId查询获取商品主图，商品规格信息，避免直接服务器端跳转方式出现刷新仍然提交表单的情况
        ModelAndView modelAndView = new ModelAndView("goods/modify_goods_pic");
        GoodsCommon goodsCommon = goodsService.getGoodsCommonById(id);
        List<GoodsImages> imagesList = goodsService.getGoodsImageByCommonId(goodsCommon.getGoodsCommonid());
        if("N;".equals(goodsCommon.getSpecValue())){
            logger.debug("goods_"+id+" has spec:"+1);
            modelAndView.addObject("specNum",0);

        }else{
            String[] specNames = goodsCommon.getSpecName().split(",");
            Map<String,Object> valueList = JSON.parseObject(goodsCommon.getSpecValue());
            Object array =  valueList.get(specNames[0]);
            modelAndView.addObject("specName", specNames[0]);
            modelAndView.addObject("specNum", array);
            //TODO 解析spec_value字段获取共有几个规格
        }
        modelAndView.addObject("goods",goodsCommon);
        modelAndView.addObject("goodsCommonid",id);
        modelAndView.addObject("imagesList",imagesList);
        return modelAndView;
    }

//    /**
//     * 商品图片多图上传
//     * @param pics
//     * @param flag
//     * @param goodsCommonId
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "doWithPictures")
//    public ModelAndView doWithPictures(@RequestParam String[] pics,String flag,String goodsCommonId,HttpServletResponse response){
//        this.goodsService.doWithPictures(pics,goodsCommonId);
//        if("editor".equals(flag))
//            return new ModelAndView("goods/modify_goods");
//        else
//            return new ModelAndView("goods/modify_goods");
//    }


    @RequestMapping("toModifyGoodsClass")
    public ModelAndView toModifyGoodsClass(Integer goodsCommonId,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("goods/modify_goods_class");

        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

//        List<Goss> goodsClassList =  goodsService.publishGoods();
        //改为从redis中获取数据
//        List<GoodsClass> goodsClassList = goodsClassRedisService.getGoodsClassList(null);
        List<GoodsClass> goodsClassList = goodsClassService.findStoreBindClass(store.getStoreId(), 1,null);
        //获取商户常用分类
        GoodsClassStaple goodsClassStaple = new GoodsClassStaple();
        goodsClassStaple.setMemberId(store.getMemberId());
        List<GoodsClassStaple> goodsClassStapleList =goodsClassStapleMapper.findClassStapleByCondition(goodsClassStaple);
        modelAndView.addObject("goodsClassList",goodsClassList);
        modelAndView.addObject("goodsClassStapleList",goodsClassStapleList);
        modelAndView.addObject("goodsCommonId",goodsCommonId);
        return modelAndView;
    }



    @RequestMapping("doModifyGoodsClass")
    public ModelAndView doModifyGoodsClass(String gc1,String gc2,String category,String gcName ,Integer goodsCommonId,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("redirect:toModifyGoods?id="+goodsCommonId);
        GoodsCommon goodsCommon = new GoodsCommon();
        goodsCommon.setGcId1(Integer.parseInt(gc1));
        goodsCommon.setGcId2(Integer.parseInt(gc2));
        goodsCommon.setGcName(gcName);
        goodsCommon.setGoodsCommonid(goodsCommonId);
        goodsService.modifyGoodsCommonClass(goodsCommon);
        return modelAndView;
    }

    /**
     * 保存规格值
     * @param specValue
     * @return
     */

    @RequestMapping("saveSpecValue")
    @ResponseBody
    public String saveSpecValue(HttpServletRequest request,SpecValue specValue){
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        specValue.setStoreId(store.getStoreId());
        specValue.setSpValueSort(0);
        if (specValueMapper.insert(specValue)==1){
//            Map<String,Object> map = new HashMap<String,Object>();
//            map.put("done",true);
            return "true";
        }else {
            return "false";
        }
    }

    /**
     * 分类列表
     *
     * @return
     */
    @RequestMapping("gclass")
    public ModelAndView goods_class(@RequestParam(required = false, defaultValue = "1") int start,
                                    @RequestParam(required = false, defaultValue = "") String gcName) {
        ModelAndView modelAndView = new ModelAndView("goods/class/class_list");

        GoodsClass gc = new GoodsClass();
        gc.setStart(start);
        if (!"".equals(gcName)) {
            gc.setGcName(gcName);
        }
        gc.setResultList(goodsClassMapper.findGoodsClass(gc));
        modelAndView.addObject("page", gc);
        return modelAndView;
    }

    /**
     * to-------增加商品种类
     *
     * @return
     */
    @RequestMapping(value = "gclass/toAdd")
    public ModelAndView gclassAdd(@RequestParam(required = false, defaultValue = "0") String gcParentId) {
        ModelAndView modelAndView = new ModelAndView("goods/class/class_add");

        List<GoodsClass> goodsClassListAll = new ArrayList<GoodsClass>();

        if ("0".equals(gcParentId)) {
//            List<GoodsClass> level1Gc = goodsClassRedisService.getGoodsClassList(null);
            List<GoodsClass> level1Gc = goodsClassMapper.findGoodsClass(new GoodsClass(0, false));
            for (GoodsClass goodsClass1 : level1Gc) {
                goodsClassListAll.add(goodsClass1);
//                List<GoodsClass> level2Gc =goodsClassRedisService.getGoodsClassList(goodsClass1.getGcId());
                List<GoodsClass> level2Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass1.getGcId()));
                goodsClassListAll.addAll(level2Gc);
                for (GoodsClass goodsClass2 : level2Gc) {
                    goodsClass2.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass2.getGcName());
                }
            }

        } else {
            List<GoodsClass> level2Gc = goodsClassRedisService.getGoodsClassList(Integer.parseInt(gcParentId));
            goodsClassListAll.addAll(level2Gc);
        }

        modelAndView.addObject("goodsClassList", goodsClassListAll);
//
//
//        if(null != gcParentId && !"".equals(gcParentId)){
//            modelAndView.addObject("gcParentId",gcParentId);
//        }
        return modelAndView;
    }

    /**
     * do------新增分类
     */
    @RequestMapping("gclass/doAdd")
    public ModelAndView addGoodClass(MultipartHttpServletRequest multipartHttpServletRequest) {

        ModelAndView modelAndView = new ModelAndView("message");
        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGcName(multipartHttpServletRequest.getParameter("gcName"));

        goodsClass.setCommisRate(multipartHttpServletRequest.getParameter("commisRate") == null || multipartHttpServletRequest.getParameter("commisRate").equals("") ? 0 : Float.parseFloat(multipartHttpServletRequest.getParameter("commisRate")));
        goodsClass.setCommisRateSecond(multipartHttpServletRequest.getParameter("commisRateSecond") == null || multipartHttpServletRequest.getParameter("commisRateSecond").equals("") ? 0 : Float.parseFloat(multipartHttpServletRequest.getParameter("commisRateSecond")));
        goodsClass.setGcParentId(Integer.parseInt(multipartHttpServletRequest.getParameter("gcParentId")));
        goodsClass.setGcSort(Integer.parseInt(multipartHttpServletRequest.getParameter("gcSort")));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic!=null&&pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            goodsClass.setGcImg(picture);
        }

        MultipartFile pic2 = multipartHttpServletRequest.getFile("pic2");
        if (pic2!=null&&pic2.getSize() > 0) {
            String picture2 = super.uploadPicsWithoutCompressSize(pic2, multipartHttpServletRequest, null, null);
            goodsClass.setAdvImg(picture2);
        }
        goodsClass.setGcId(0);
        goodsClass.setTypeId(1);
        goodsClass.setTypeName("");
        goodsClass.setGcTitle("");
        goodsClass.setGcKeywords("");
        goodsClass.setGcDescription("");
        int i = goodsClassMapper.insert(goodsClass);
        if (i > 0) {
            modelAndView.addObject("msg", "商品分类新增成功");
        } else {
            modelAndView.addObject("msg", "商品分类新增失败");
        }
        return modelAndView;
    }

    @RequestMapping("gclass/toModify")
    public ModelAndView toModifyGoodClass(Integer gcId) {
        ModelAndView modelAndView = new ModelAndView("goods/class/class_modify");
        GoodsClass goodsClass = this.goodsClassMapper.selectByPrimaryKey(gcId);

//        List<GoodsClass> goodsClassList = goodsClassMapper.findGoodsClass(new GoodsClass());
//        modelAndView.addObject("goodsClassList", goodsClassList);
        List<GoodsClass> goodsClassListAll = new ArrayList<GoodsClass>();
        List<GoodsClass> level1Gc = goodsClassMapper.findGoodsClass(new GoodsClass(0, false));
        for (GoodsClass goodsClass1 : level1Gc) {
            goodsClassListAll.add(goodsClass1);
            List<GoodsClass> level2Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass1.getGcId()));
            goodsClassListAll.addAll(level2Gc);
            for (GoodsClass goodsClass2 : level2Gc) {
                goodsClass2.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass2.getGcName());
            }
        }
        modelAndView.addObject("goodsClassList", goodsClassListAll);
        modelAndView.addObject("goodsClass", goodsClass);
        return modelAndView;
    }


    @RequestMapping("gclass/doModify")
    public ModelAndView doModifyGoodClass(MultipartHttpServletRequest multipartHttpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("message");

        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGcId(Integer.parseInt(multipartHttpServletRequest.getParameter("gcId")));
        goodsClass.setGcParentId(Integer.parseInt(multipartHttpServletRequest.getParameter("gcParentId")));
//        goodsClass.setGcName(multipartHttpServletRequest.getParameter("gcName"));
        goodsClass.setCommisRate(multipartHttpServletRequest.getParameter("commisRate") == null || multipartHttpServletRequest.getParameter("commisRate").equals("") ? 0 : Float.parseFloat(multipartHttpServletRequest.getParameter("commisRate")));
        goodsClass.setCommisRateSecond(multipartHttpServletRequest.getParameter("commisRateSecond") == null ||multipartHttpServletRequest.getParameter("commisRateSecond").equals("") ? 0 : Float.parseFloat(multipartHttpServletRequest.getParameter("commisRateSecond")));
        goodsClass.setGcSort(Integer.parseInt(multipartHttpServletRequest.getParameter("gcSort")));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic!=null&&pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            goodsClass.setGcImg(picture);
        }

        MultipartFile pic2 = multipartHttpServletRequest.getFile("pic2");
        if (pic2!=null&&pic2.getSize() > 0) {
            String picture2 = super.uploadPicsWithoutCompressSize(pic2, multipartHttpServletRequest, null, null);
            goodsClass.setAdvImg(picture2);
        }
        goodsClass.setTypeId(1);//暂时先用固定1 登录功能完成后从session中取
        goodsClass.setTypeName("");
        goodsClass.setGcTitle("");
        goodsClass.setGcKeywords("");
        goodsClass.setGcDescription("");
        int i = goodsClassMapper.updateByPrimaryKeySelective(goodsClass);
        if (i > 0) {
            modelAndView.addObject("msg", "商品分类修改成功");
        } else {
            modelAndView.addObject("msg", "商品分类修改失败");
        }

        return modelAndView;
    }

    /**
     * 删除类别
     */
    @RequestMapping("gclass/delGclass")
    public ModelAndView delGclass(HttpServletRequest request) {
        String[] gcIds = request.getParameterValues("gcId");
        ModelAndView modelAndView = new ModelAndView("message");
        try {
            for (String id : gcIds) {
                goodsClassMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
            modelAndView.addObject("msg", "商品分类删除成功");
            modelAndView.addObject("url", "gclass");
        } catch (Exception e) {
            e.printStackTrace();
            modelAndView.addObject("msg", "商品分类删除失败");
        }
        return modelAndView;
    }

    /**
     * 分类名称检查是否重复
     *
     * @return
     */
    @RequestMapping("gclass/checkGClassName")
    public
    @ResponseBody
    boolean checkGClassName(String gcName) {
        GoodsClass gClass = new GoodsClass();
        gClass.setGcName(gcName);
        List<GoodsClass> goodClassList = this.goodsClassMapper.findGoodsClass(gClass);
        if (goodClassList.size() > 0) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * 品牌列表
     *
     * @return
     */
    @RequestMapping("brand")
    public ModelAndView list(Brand brand) {
        ModelAndView modelAndView = new ModelAndView("goods/brand/brand_list");
        List<Brand> brandList = brandMapper.findBrandByCondition(brand);
        brand.setResultList(brandList);
        modelAndView.addObject("page", brand);
        return modelAndView;
    }

    /**
     * to-------增加品牌
     *
     * @return
     */
    @RequestMapping("brand/toAdd")
    public ModelAndView brandAdd() {
        ModelAndView modelAndView = new ModelAndView("goods/brand/brand_add");
        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGcParentId(0);
        List<GoodsClass> goodsClassList = goodsClassMapper.findGoodsClass(goodsClass);
        modelAndView.addObject("goodsClassList", goodsClassList);
        return modelAndView;
    }

    /**
     * 品牌名称检查是否重复
     *
     * @param brandName
     * @return
     */
    @RequestMapping("brand/checkBrandName")
    public
    @ResponseBody
    boolean checkBrandName(String brandName) {
        Brand brand = new Brand();
        brand.setBrandName(encodeStr(brandName));
        List<Brand> brandList = this.brandMapper.findBrandByCondition(brand);
        if (brandList.size() > 0) {
            return false;
        } else {
            return true;
        }


    }


    /**
     * do------新增品牌
     */
    @RequestMapping("brand/doAdd")
    public ModelAndView add(MultipartHttpServletRequest multipartHttpServletRequest) {

        ModelAndView modelAndView = new ModelAndView("message");
        Brand brand = new Brand();
        brand.setBrandName(multipartHttpServletRequest.getParameter("brandName"));
        brand.setBrandClass(multipartHttpServletRequest.getParameter("brandClass"));
        brand.setBrandInitial(multipartHttpServletRequest.getParameter("brandInitial"));
//        brand.setBrandPic(multipartHttpServletRequest.getParameter("brandClass"));
        brand.setBrandRecommend(Byte.parseByte(multipartHttpServletRequest.getParameter("brandRecommend")));
        brand.setBrandSort(Integer.parseInt(multipartHttpServletRequest.getParameter("brandSort")));
        brand.setStoreId(1); //暂时先用固定1 登录功能完成后从session中取
        brand.setBrandApply((byte) 0);
        String classId = multipartHttpServletRequest.getParameter("classId");
        if(classId!=null&&!classId.equals("")){
            brand.setClassId(Integer.parseInt(multipartHttpServletRequest.getParameter("classId")));
        }
        brand.setWapIndex(multipartHttpServletRequest.getParameter("wapIndex"));
        MultipartFile BrandPicturempf = multipartHttpServletRequest.getFile("brandPicture");
        if(BrandPicturempf.getSize()>0){
            String BrandPicture = super.uploadPicsWithoutCompressSize(BrandPicturempf, multipartHttpServletRequest, null, null);
            brand.setBrandPic(BrandPicture);
        }
        MultipartFile wapAdvPicmpf = multipartHttpServletRequest.getFile("wapAdvPic");
        if(wapAdvPicmpf.getSize()>0){
            String wapAdvPic = super.uploadPicsWithoutCompressSize(wapAdvPicmpf, multipartHttpServletRequest, null, null);
            brand.setWapAdvPic(wapAdvPic);
        }
        brand.setWapAdvUrl(multipartHttpServletRequest.getParameter("wapAdvUrl"));
        int i = brandMapper.insert(brand);
        if (i > 0) {
            modelAndView.addObject("msg", "商品品牌新增成功");
        } else {
            modelAndView.addObject("msg", "商品品牌新增失败");
        }
        return modelAndView;
    }

    @RequestMapping("brand/toModify")
    public ModelAndView toModifyBrand(Integer brandId) {
        ModelAndView modelAndView = new ModelAndView("goods/brand/brand_modify");
        Brand brand = this.brandMapper.selectByPrimaryKey(brandId);
        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGcParentId(0);
        goodsClass.setPageSwitch(false);
        List<GoodsClass> goodsClassList = goodsClassMapper.findGoodsClass(goodsClass);
        modelAndView.addObject("goodsClassList", goodsClassList);
        modelAndView.addObject("brand", brand);
        return modelAndView;
    }

    @RequestMapping("brand/editbrandRecommend")
    public ModelAndView editbrandRecommend(int id, Byte brandRecommend) {
        ModelAndView modelAndView = new ModelAndView("redirect:/goods/brand");
        Brand brand = brandMapper.selectByPrimaryKey(id);
        brand.setBrandRecommend(brandRecommend);
        brandMapper.updateByPrimaryKeySelective(brand);
        return modelAndView;
    }


    @RequestMapping("brand/doModify")
    public ModelAndView doModifyBrand(MultipartHttpServletRequest multipartHttpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("message");

        Brand brand = new Brand();
        brand.setBrandId(Integer.parseInt(multipartHttpServletRequest.getParameter("brandId")));
        brand.setBrandName(multipartHttpServletRequest.getParameter("brandName"));
        brand.setBrandClass(multipartHttpServletRequest.getParameter("brandClass"));
        brand.setBrandInitial(multipartHttpServletRequest.getParameter("brandInitial"));
        brand.setBrandRecommend(Byte.parseByte(multipartHttpServletRequest.getParameter("brandRecommend")));
        brand.setBrandSort(Integer.parseInt(multipartHttpServletRequest.getParameter("brandSort")));
        brand.setWapIndex(multipartHttpServletRequest.getParameter("wapIndex"));
        brand.setStoreId(1); //暂时先用固定1 登录功能完成后从session中取
        brand.setBrandApply((byte) 0);
        String classId = multipartHttpServletRequest.getParameter("classId");
        if(classId!=null&&!classId.equals("")){
            brand.setClassId(Integer.parseInt(multipartHttpServletRequest.getParameter("classId")));
        }
        MultipartFile BrandPicturempf = multipartHttpServletRequest.getFile("brandPicture");
        if (BrandPicturempf.getSize() > 0) {
            String BrandPicture = super.uploadPicsWithoutCompressSize(BrandPicturempf, multipartHttpServletRequest, null, null);
            brand.setBrandPic(BrandPicture);
        }
        MultipartFile wapAdvPicmpf = multipartHttpServletRequest.getFile("wapAdvPic");
        if (wapAdvPicmpf.getSize() > 0) {
            String wapAdvPic = super.uploadPicsWithoutCompressSize(wapAdvPicmpf, multipartHttpServletRequest, null, null);
            brand.setWapAdvPic(wapAdvPic);
        }
        brand.setWapAdvUrl(multipartHttpServletRequest.getParameter("wapAdvUrl"));
        int i = brandMapper.updateByPrimaryKeySelective(brand);
        if (i > 0) {
            modelAndView.addObject("msg", "商品品牌修改成功");
        } else {
            modelAndView.addObject("msg", "商品品牌修改失败");
        }

        return modelAndView;
    }


    @RequestMapping("brand/delBrand")
    public ModelAndView delBrand(HttpServletRequest request) {
        String[] brandId = request.getParameterValues("brandIds");
        int i = 0;
        for (String id : brandId) {
            i += brandMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        ModelAndView modelAndView = new ModelAndView("message");
        if (i == brandId.length) {
            modelAndView.addObject("msg", "商品品牌删除成功");
        } else {
            modelAndView.addObject("msg", "商品品牌删除失败");
        }
        return modelAndView;
    }
}
